#include <stdio.h>
#include <unistd.h>
int main(void)
{
	int fp[2];
	if(pipe(fp) == -1)
	{
		perror("create pipe error");
		return 1;
	}
	int pid = fork();
	int i = 0;
	if(pid == -1)
	{
		perror("fork error");
		return 1;
	}
	else if(pid > 0)
	{
		// 父进程写入，先关闭管道读端
		close(fp[0]);
		i = 8;
		if(sizeof(int) != write(fp[1],(void*)&i,sizeof(int)))
		{
			perror("first write error");
			return 1;
		}
		i = 88;
		if(sizeof(int) != write(fp[1],(void*)&i,sizeof(int)))
		{
			perror("second write error");
			return 1;
		}
		wait(NULL);
		close(fp[1]);
	}
	else if(pid == 0)
	{
		// 子进程读，先关闭管道写端
		close(fp[1]);
		int j = 0;
		if(-1 == read(fp[0],(void*)&j,sizeof(int)))
		{
			perror("first read error");
			return 1;
		}
		printf("first read;i = %d\n",j);
		if(-1 == read(fp[0],(void*)&j,sizeof(int)))
		{
			perror("second read error");
			return 1;
		}
		printf("second read;i = %d\n",j);
		close(fp[0]);
	}
	
	return 0;
}
